@page "/admin"
@rendermode InteractiveServer
@using DaoHang.Services
@using DaoHang.Models
@using Microsoft.AspNetCore.Components.Authorization
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject ISiteSettingsService SiteSettingsService
@inject IJSRuntime JSRuntime

<PageTitle>后台管理 - 导航网站</PageTitle>

@if (isAuthenticated)
{
<div class="container-fluid py-4">
    <!-- 后台管理标题 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">
            <i class="bi bi-gear-fill me-2"></i>后台管理
        </h1>
        <div class="d-flex align-items-center gap-3">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="/">首页</a></li>
                    <li class="breadcrumb-item active">后台管理</li>
                </ol>
            </nav>
            <button class="btn btn-outline-light btn-sm" @onclick="Logout">
                <i class="bi bi-box-arrow-right me-1"></i>退出登录
            </button>
        </div>
    </div>

    <!-- 管理导航标签 -->
    <ul class="nav nav-tabs mb-4" id="adminTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "websites" ? "active" : "")" 
                    @onclick='() => SetActiveTab("websites")'
                    type="button">
                <i class="bi bi-globe me-2"></i>网站管理
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "categories" ? "active" : "")" 
                    @onclick='() => SetActiveTab("categories")'
                    type="button">
                <i class="bi bi-grid-3x3-gap me-2"></i>分类管理
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "icons" ? "active" : "")" 
                    @onclick='() => SetActiveTab("icons")'
                    type="button">
                <i class="bi bi-image me-2"></i>图标管理
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "data-export" ? "active" : "")" 
                    @onclick='() => SetActiveTab("data-export")'
                    type="button">
                <i class="bi bi-download me-2"></i>数据管理
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "logs" ? "active" : "")" 
                    @onclick='() => SetActiveTab("logs")'
                    type="button">
                <i class="bi bi-journal-text me-2"></i>日志查看
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link @(activeTab == "settings" ? "active" : "")" 
                    @onclick='() => SetActiveTab("settings")'
                    type="button">
                <i class="bi bi-gear me-2"></i>网站设置
            </button>
        </li>
    </ul>

    <!-- 标签页内容 -->
    <div class="tab-content">
        <!-- 网站管理 -->
        @if (activeTab == "websites")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.WebsiteManagement />
            </div>
        }

        <!-- 分类管理 -->
        @if (activeTab == "categories")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.CategoryManagement />
                            </div>
                        }

        <!-- 图标管理 -->
        @if (activeTab == "icons")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.IconManagement />
            </div>
        }

        <!-- 数据管理 -->
        @if (activeTab == "data-export")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.DataManagement />
            </div>
        }

        <!-- 日志查看 -->
        @if (activeTab == "logs")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.LogManagement />
            </div>
        }

        <!-- 网站设置 -->
        @if (activeTab == "settings")
        {
            <div class="tab-pane fade show active">
                    <DaoHang.Components.Admin.SiteSettingsManager />
                                </div>
                            }
                                    </div>
                                </div>
}
else if (isLoading)
{
    <div class="container py-5 text-center">
        <div class="spinner-border" role="status">
            <span class="visually-hidden">加载中...</span>
                            </div>
        <p class="mt-3">正在验证登录状态...</p>
                                </div>
                                    }
                                    else
                                    {
    <div class="container py-5 text-center">
        <h2 class="mb-4">需要登录</h2>
        <p class="lead">您需要登录才能访问管理后台。</p>
        <a href="@($"/{adminRoute}/login")" class="btn btn-primary">前往登录</a>
            </div>
        }

@code {
    /// <summary>
    /// 当前活动的标签页
    /// </summary>
    private string activeTab = "websites";

    /// <summary>
    /// 是否已认证
    /// </summary>
    private bool isAuthenticated = false;

    /// <summary>
    /// 是否正在加载
    /// </summary>
    private bool isLoading = true;

    /// <summary>
    /// 管理路由
    /// </summary>
    private string adminRoute = "admin";

    /// <summary>
    /// 设置活动标签页
    /// </summary>
    /// <param name="tab">标签页名称</param>
    private void SetActiveTab(string tab)
    {
        activeTab = tab;
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    private void Logout()
    {
        Navigation.NavigateTo("/api/auth/logout", true);
    }

    /// <summary>
    /// 组件初始化时检查登录状态
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        try
        {
            // 获取站点设置中的管理路由
            var siteSettings = await SiteSettingsService.GetSiteSettingsAsync();
            adminRoute = siteSettings?.AdminRoute ?? "admin";

            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            var user = authState.User;
            
            isAuthenticated = user?.Identity?.IsAuthenticated == true;
            
            if (!isAuthenticated)
            {
                // 使用JavaScript重定向以确保在反向代理环境下保持原始域名
                await RedirectToLogin();
                return;
            }
        }
        catch (Exception)
        {
            // 认证状态获取失败，重定向到登录页面
            await RedirectToLogin();
                return;
        }
        finally
        {
            isLoading = false;
        }
    }

    /// <summary>
    /// 重定向到登录页面
    /// </summary>
    private async Task RedirectToLogin()
    {
        try
        {
            // 使用JavaScript重定向，保持当前域名
            var loginPath = $"/{adminRoute}/login";
            await JSRuntime.InvokeVoidAsync("window.location.assign", loginPath);
        }
        catch (Exception)
        {
            // JavaScript执行失败时回退到NavigationManager
            Navigation.NavigateTo($"/{adminRoute}/login", true);
        }
    }
} 